![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
Weve covered a lot of territory so far in this book. Now we can put some of your newly gained knowledge to use. In this chapter, we will explore what it takes to develop a JDBC driver. In doing so, we will also touch on some of the finer points of the JDBC specification. Throughout this chapter, I will use excerpts from the SimpleText JDBC driver that is included on the CD-ROM. This driver allows you to manipulate simple text files; you will be able to create and drop files, as well as insert and select data within a file. The SimpleText driver is not fully JDBC-compliant, but it provides a strong starting point for developing a driver. Well cover what the JDBC components provide, how to implement the JDBC API interfaces, how to write native code to bridge to an existing non-Java API, some finer points of driver writing, and the major JDBC API interfaces that must be implemented.
The SimpleText JDBC driver is just thata JDBC driver that manipulates simple text files, with a few added twists. It is not a full-blown relational database system, so I would not recommend attempting to use it as one. If you are looking for a good way to prototype a system, or need a very lightweight database system to drive a simplistic application or applet, then SimpleText is for you. More importantly, though, the SimpleText driver can serve as a starting point for your own JDBC driver. Before continuing, lets take a look at the SimpleText driver specifications.
The SimpleText JDBC driver supports a very limited SQL grammar. This is one reason that the driver is not JDBC compliant; a JDBC-compliant driver must support ANSI92 entry level SQL grammar. The following SQL statements define the base SimpleText grammar:
create-table-statement ::= CREATE TABLE table-name (column-element [, column-element]...) drop-table-statement ::= DROP TABLE table-name insert-statement ::= INSERT INTO table-name [(column-identifier [, column-identifier]...)] VALUES (insert-value [, insert-value]...) select-statement ::= SELECT select-list FROM table-name [WHERE search- condition]
The following elements are used in these SQL statements:
column-element ::= column-identifier data-type column-identifier ::= user-defined-name comparison-operator ::= < | > | = | <> data-type ::= VARCHAR | NUMBER | BINARY dynamic-parameter ::= ? insert-value ::= dynamic-parameter | literal search-condition ::= column-identifier comparison-operator literal select-list ::= * | column-identifier [, column-identifier]... table-name ::= user-defined-name user-defined-name ::= letter [digit | letter]
What all this grammar means is that the SimpleText driver supports a CREATE TABLE statement, a DROP TABLE statement, an INSERT statement (with parameters), and a very simple SELECT statement (with a WHERE clause). It may not seem like much, but this grammar is the foundation that will allow us to create a table, insert some data, and select it back.
The format of the files used by the SimpleText driver is, of course, very simple. The first line contains a signature, followed by each one of the column names (and optional data types). Any subsequent lines in the text file are assumed to be comma-separated data. There is no size limit to the text file, but the larger the file, the longer it takes to retrieve data (the entire file is read when selecting data; there is no index support). The data file extension is hard coded to be .SDF (Simple Data File). For example, the statement
CREATE TABLE TEST (COL1 VARCHAR, COL2 NUMBER, COL3 BINARY)
creates a file named TEST.SDF, with the following initial data:
.SDFCOL1,#COL2,@COL3
Note that none of the SQL grammar is case-sensitive. The .SDF is the file signature (this is how the SimpleText driver validates whether the text file can be used), followed by a comma-separated list of column names. The first character of the column name can specify the data type of the column. A column name starting with a # indicates a numeric column, while a column name starting with an @ indicates a binary column. Whats that? Binary data in a text file? Well, not quite. A binary column actually contains an offset pointer into a sister file. This file, with an extension of .SBF (Simple Binary File), contains any binary data for columns in the text file, as well as the length of the data (maximum length of 1048576 bytes). Any other column name is considered to be character data (with a maximum length of 5120 bytes). The following statement shows how data is inserted into the TEST table:
INSERT INTO TEST VALUES ('FOO', 123, '0123456789ABCDEF')
After the INSERT, TEST.SDF will contain the following data:
.SDFCOL1,#COL2,@COL3 FOO,123,0
COL3 contains an offset of zero since this is the first row in the file. This is the offset from within the TEST.SBF table in which the binary data resides. Starting at the given offset, the first four bytes will be the length indicator, followed by the actual binary data that was inserted. Note that any character or binary data must be enclosed in single quotation marks.
Well be looking at plenty of code from the SimpleText driver throughout this chapter. But first, lets start by exploring what is provided by the JDBC developers kit.
The JDBC DriverManager is a static class that provides services to connect to JDBC drivers. The DriverManager is provided by JavaSoft and does not require the driver developer to perform any implementation. Its main purpose is to assist in loading and initializing a requested JDBC driver. Other than using the DriverManager to register a JDBC driver (registerDriver) to make itself known and to provide the logging facility (which is covered in detail later), a driver does not interface with the DriverManager. In fact, once a JDBC driver is loaded, the DriverManager drops out of the picture all together, and the application or applet interfaces with the driver directly.
Previous | Table of Contents | Next |